import { UserData, UserState } from "../game.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js"
import { Story } from "../model/story.js";
import { tt } from "../../ttapi_interface/ttapi.js";
export class State_Story extends UserState {
    constructor(storyurl: string, nextstate: tt2.QFrame_IState<UserData>) {
        super()
        this._storyurl = storyurl;
        this._nextstate = nextstate;
    }
    private _nextstate: tt2.QFrame_IState<UserData> = null;
    private _storyurl: string;
    private _inited: boolean = false;

    OnInit(statemgr: tt2.QFrame_App<UserData>): void {
        super.OnInit(statemgr);
        this.InitAsync();
    }
    OnExit(): void {
        this.story.Destory();
        let _ui = this.userdata.scene.GetUI();
        _ui.removeChildAll();

    }

    private story: Story
    private _gamebtn: tt2.QUI_Button;
    private _btnborder: tt.Sprite;
    private _endtimer: number = 0;;
    private _end: boolean = false;
    async InitAsync(): Promise<void> {

        this.story = new Story(this.userdata.scene);
        await this.story.LoadAsync(this._storyurl);
        this._btnborder = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("btnborder.png");

        this._inited = true;
    }

    OnUpdate(delta: number): void {
        super.OnUpdate(delta);
        if (!this._inited)
            return;
        this.story.OnUpdate(delta);
        if (this.story.IsFinish() && this._gamebtn == null) {

            let btn = this._gamebtn = new tt2.QUI_Button();
            btn.localRect.setHPosByCenter(160);
            btn.localRect.setVPosByBottomBorder(40, 30);
            let bimg: tt2.QUI_ImageScale9;
            {

                let s9 = new tt2.QUI_Scale9(this._btnborder, new tt.Border(4, 4, 4, 4));
                let label = new tt2.QUI_Label(this.app.font, "开始");
                label.fontScale.X = 2;
                label.fontScale.Y = 2;
                label.color = new tt.Color(0.3, 0.3, 0.3, 1);
                bimg = new tt2.QUI_ImageScale9(s9);
                label.localRect.setAsFill();
                bimg.addChild(label);
                bimg.localRect.setAsFill();
            }
            let bimg2 = bimg.Clone() as tt2.QUI_ImageScale9;
            bimg2.color = new tt.Color(0.5, 0.5, 0.5, 1.0);

            //bimg2.localRect.setAsFill();
            btn.ElemNormal = bimg;
            btn.ElemPress = bimg2;


            this.userdata.scene.GetUI().addChild(btn);
            btn.OnClick = () => {
                this.evt_ToGame();
            }
        }
        if (this._end) {
            this._endtimer -= delta;
            if (this._endtimer <= 0) {
                this.app.ChangeState(this._nextstate);
                return;
            }
        }
    }

    //事件开始游戏
    evt_ToGame() {
        // for (var i in this.story.activeItems) {
        //     let etime = this.story.activeItems[i].line.fadeouttime;
        //     if (this._endtimer < etime)
        //         this._endtimer = etime;

        // }

        this._endtimer = this.story.FadeOutAll();
        this._end = true;


    }
}